﻿using System;
using UnityEngine;

namespace CGF
{
    public static class DeviceHelper
    {
        //是否是刘海屏
        public static bool HasDisplayCutout()
        {
            if (Application.platform != RuntimePlatform.Android) return false;
            if (GetAndroidSDKVersion() < 28) return false;

            try
            {
                using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
                {
                    AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
                    AndroidJavaObject windowManager = activity.Call<AndroidJavaObject>("getSystemService", "window");
                    if (windowManager == null) return false;
                    using (var display = windowManager.Call<AndroidJavaObject>("getDefaultDisplay"))
                    {
                        var cutout = display.Call<AndroidJavaObject>("getCutout");
                        return cutout != null;
                    }
                }
            }
            catch (Exception e)
            {
                Debug.LogError("Failed to check display cutout: " + e.Message);
                return false;
            }
        }
        
        // 获取Android SDK版本
        public static int GetAndroidSDKVersion()
        {
            try
            {
                using (var version = new AndroidJavaClass("android.os.Build$VERSION"))
                {
                    return version.GetStatic<int>("SDK_INT");
                }
            }
            catch (Exception e)
            {
                Debug.LogError("获取 Android SDK 版本失败: " + e.Message);
                return 0;
            }
        }
    }
}